Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Conversation

mike-kfed
Copy link

Talking on discord I've learned that directly accessing wasm's linear memory to efficiently share data between the universes is not necessary anymore, because js_sys now has a view method for JavaScripts typed Arrays that does exactly that. To help other coders know about that feature, and more obviously show that this is unsafe code potentially, I adapted the code to use the view instead.

self.cells.as_ptr()
pub fn cells(&self) -> js_sys::Uint8Array {
unsafe {
let u8_cells = mem::transmute::<&Vec<Cell>, &Vec<u8>>(&self.cells);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can avoid transmute here and instead cast the slice to pointer, convert the pointer, and construct a new slice from that:

    let u8_cells: &[u8] = unsafe {
        let ptr = self.cells.as_ptr() as *const u8;
        let bytelen = self.cells.len() * std::mem::size_of::<Cell>();
        std::slice::from_raw_parts(ptr, bytelen)
    };

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=98b83b545fce22aec8fdf4a5e2ed5c55

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants